<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<jsp:useBean id="player" class="pe.lucene.web.myrating.Player" scope="session"/>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="org.hibernate.Session"%>
<%@page import="pe.lucene.web.myrating.HibernateUtil"%>
<%@page import="pe.lucene.web.myrating.Rating"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<link rel="stylesheet" type="text/css" href="index.css" />
<title>Modify My Rating</title>
</head>
<script type="text/javascript"><!--

var ratingResult = 5;

function minusRating() {

   if (ratingResult -1 < 0 || ratingResult -1 > 10) {
       return ;
   }
    
    ratingResult--;
    printImg();
}

function addRating() {

   if (ratingResult + 1 < 0 || ratingResult + 1 > 10) 
        return ;

    ratingResult++;
    printImg();
}


function printImg() {

    if (ratingResult < 0 || ratingResult > 10) {
        return ;
    }

    var holder = document.getElementById("ratingarea");
    var now_holder_count = holder.childNodes.length;

    /*
     * count expect #text, SCRIPT
     */
    for (var hcount = 0; hcount < holder.childNodes.length; hcount++ ) {
        var nodeItem = holder.childNodes.item(hcount);

        if (nodeItem.nodeName == 'SCRIPT' || nodeItem.toString() == 'object HTMLScriptElement') {
            now_holder_count--;
            holder.removeChild(nodeItem);
        }
        
        if (nodeItem.nodeName == '#text') {
            now_holder_count--;
        }
    }
    
    if (document.getElementById("ratingvalue") != null) {
        document.getElementById("ratingvalue").setAttribute("value", ratingResult);
    }

    if (now_holder_count == ratingResult) {
        // nothing
    } else if (now_holder_count > ratingResult) {
        for (var count = 0; count < now_holder_count - ratingResult; count++) {

        	for (var removeCount = 0; removeCount < now_holder_count; removeCount++) {
                if (holder.lastChild.nodeName == '#text')
                    holder.removeChild(holder.lastChild);
                else {
                    holder.removeChild(holder.lastChild);
                    break;
                }
            }
            
        }
        
    } else if (now_holder_count < ratingResult) {
        for (var count = 0; count < ratingResult - now_holder_count ; count++) {
            var img = document.createElement("img");
            img.src ="../image/rating_star_basic.png";
            
            holder.appendChild(img);
        }
    }
}

function initratingResult(value) {
	ratingResult = value;
	printImg();
}
--></script>
<link rel="stylesheet" type="text/css" href="../index.css" />
<link rel="stylesheet" type="text/css" href="../table.css" />
<link rel="stylesheet" type="text/css" href="../input.css" />
<body>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
    <!-- title -->
    <tr background="../image/back_titlebar.png" height="30">
        <td >MyRating</td>
        <td align="right">ver 0.1</td>
    </tr>
    <!-- Decoration empty bar -->
    <tr>
        <td colspan="2" height="2" bgcolor="#959aa0"></td>
    </tr>
    
    <!-- from  -->

	<%
	Session hbSession = null;
	
	try {
	    hbSession = HibernateUtil.currentSession();
	    String num = request.getParameter("num");
	    int numInt = Integer.parseInt(num);
	    
	    Rating rating = (Rating) hbSession.get(Rating.class, new Integer(num));
	    
	    out.println("<script language='javascript'>");
	    out.println("initratingResult(" + rating.getRating() + ");");
	    out.println("</script>");
	    
	    if (rating != null) {
	        %>
	          <form name="ratingform" action="update_ok.jsp">
	            <!-- id --
                <tr>
	            <td><input type="hidden" name="id" value="<%=player.getId() %>"></input></td>
	            </tr>
	            
	            <!-- num -->
	            <input type="hidden" name="num" value="<%=num %>"></input>
	            
	            <!-- Rating star -->
	            <tr>
	            <td>
	            Rating : <span onclick="minusRating()">-</span>
	            <input name="ratingvalue" type="hidden" id="ratingvalue"></input>
	            <span id="ratingarea">
	            <script type="text/javascript">
	            <!--
	            printImg();
	            //-->
	            </script>
	            </span>
	            <span onclick="addRating()">+</span>
	            </td>
	            </tr>
	            
	            <!-- Date -->
	            Date : <input id="date" type="text" name="date" value="<%=rating.getDate() %>"></input>
	            <BR>
	            
	            <!-- team -->
	            VS team : <input id="vs_team" type="text" name="vs_team" value="<%=rating.getTeam() %>"></input>
	            <BR>
	            
	            <input type="submit"></input>
	        </form>
	        <%
	    }
	    
	} catch (Exception ex) {
	    out.println("exception/" + ex.getMessage());
	} finally {
	    HibernateUtil.closeSession();
	}
	
	%>

</table>


</body>
</html>